<?php

class krt_tNG extends tNG_custom {
	var $fields = array();
	var $mxKart = null;
	var $type;
	var $sessionID;
	var $kartUID;
	
	/**
		Constructor - get a reference to Kart Recordset
	*/
	function krt_tNG($connection) {
		parent::tNG_custom($connection);
		$this->mxKart = &krt_getKartRecordset();
	}
	
	/**
		add a field to be updated in this transaction
		@params
			$fieldName - string - the kart field name
			$fieldVal - associative array - 
						uid=>value
						values for every uid
		@return
			none
	*/
	function addColumn($colName, $type, $method, $reference, $null) {
		$tmpVal = KT_getRealValue($method, $reference);
		if ($tmpVal != NULL) {
			$this->fields[$colName] = $tmpVal;
		}
	}
	
	
	function checkProperties($prPageURL, $prUrlParam) {
		// check the properties and redirect to product detail page if necessary
		$kartProps = $GLOBALS['kartProps'];
		if (!isset($_SESSION[$this->sessionID])) {
			//$this->errorNo = KT_TRANSACTION_FAILED;
			//$this->errorMsg = 'Session has expired!';
			return;
		}
		
		$id = $_SESSION[$this->sessionID]['id'];
		$price = $_SESSION[$this->sessionID]['price'];
		$properties = '';
		if (isset($_SESSION[$this->sessionID]['properties'])) {
			$properties = $_SESSION[$this->sessionID]['properties'];
		}
		if (isset($_POST['prop']) && is_array($_POST['prop'])) {
			//add Additive Fields (like Shipping Units)
			$addSql = '';
			for ($propIdx = 0;$propIdx < count($kartProps['additiveFields']);$propIdx++) {
				$addSql .= $kartProps['additiveFields'][$propIdx]['columnName'] . ', ';
			}
			$sql = "select " . $addSql. $kartProps['Kproperties']['productOptions']['priceDiff'].", ".$kartProps['Kproperties']['options']['value'].", ".$kartProps['Kproperties']['optionValues']['value']." from ".$kartProps['Kproperties']['productOptions']['tableName']." inner join ".$kartProps['Kproperties']['optionValues']['tableName']." on ".$kartProps['Kproperties']['productOptions']['optionValueId']."=".$kartProps['Kproperties']['optionValues']['pk']." inner join ".$kartProps['Kproperties']['options']['tableName']." on ".$kartProps['Kproperties']['optionValues']['optionId']."=".$kartProps['Kproperties']['options']['pk']." where ".$kartProps['Kproperties']['productOptions']['productId']."=$id and ".$kartProps['Kproperties']['productOptions']['optionValueId']." in (-1";			for ($i=0;$i<sizeof($_POST['prop']);$i++) {
				$sql .= ',' . (int)($_POST['prop'][$i]);
			}
			$sql .= ") order by ".$kartProps['Kproperties']['options']['order'].", ".$kartProps['Kproperties']['optionValues']['order'];
			$rs = $this->connection->Execute($sql);

			if ($rs) {
				while (!$rs->EOF) {
					$_SESSION[$this->sessionID]['price'] += $rs->Fields($kartProps['Kproperties']['productOptions']['priceDiff']);
					@$_SESSION[$this->sessionID]['origPriceProps'] += $rs->Fields($kartProps['Kproperties']['productOptions']['priceDiff']);
					$properties .= $rs->Fields($kartProps['Kproperties']['options']['value']) . ": " . $rs->Fields($kartProps['Kproperties']['optionValues']['value']) . "<br>";
					//add Additive Fields (like Shipping Units)
					for ($propIdx = 0;$propIdx < count($kartProps['additiveFields']);$propIdx++) {
						$myProp = $kartProps['additiveFields'][$propIdx];
						@$_SESSION[$this->sessionID][$myProp['fieldName']] += $rs->Fields($myProp['columnName']);
					}
					$rs->MoveNext();
				}
			}
		} else {
			$sql = "select count(*) as cnt from ".$kartProps['Kproperties']['productOptions']['tableName']." where ".$kartProps['Kproperties']['productOptions']['productId']."=$id";
			$rs = $this->connection->Execute($sql);
			if ($rs) {
				if ($rs->Fields('cnt') > 0) {
					$insertGoTo = $prPageURL;
					$insertGoTo = KT_addReplaceParam($insertGoTo, $prUrlParam, $id);
					KT_redir($insertGoTo);
				}
			}
		}
		if (isset($_POST['quantity'])) {
			$q = (int)$_POST['quantity'];
			if ($q <= 0) {
				$q = 1;
			}
			$_SESSION[$this->sessionID]['quantity'] = $q;
		}
		$fieldProps = array();
		while (list($key, $value) = each($kartProps['fieldProps'])) {
			$fieldProps[$value] = 1;
		}
		while (list($key, $value) = each($_POST)) {
			switch($key) {
				case 'quantity':
				case 'prop':
					break;
				default:
					if (isset($fieldProps[$key])) {
						$properties .= "$key: $value<br>\n";
					}
			}
		}
		$_SESSION[$this->sessionID]['properties'] = $properties;
	}
}

?>
